home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / vert_norm / test.c < prev   
Encoding:
C/C++ Source or Header  |  1995-02-06  |  3.6 KB  |  114 lines

  1. /* test.c - sample driver for polygon smoother */
  2. /* makes a mesh height field of quadrilaterals and triangles */
  3. /* Andrew S. Glassner / Xerox PARC */
  4.  
  5. #include "smooth.h"
  6.  
  7. #ifdef STANDALONE_TEST
  8. /* from Graphics Gems library ; for standalone compile */
  9.  
  10. /* normalizes the input vector and returns it */
  11. Vector3 *V3Normalize(Vector3 *v) {
  12.   double len = sqrt(V3Dot(v, v));
  13.   if (len != 0.0) { v->x /= len;  v->y /= len; v->z /= len; }
  14.   return(v);
  15.   }
  16.  
  17. /* return vector sum c = a+b */
  18. Vector3 *V3Add(Vector3 *a, Vector3 *b, Vector3 *c) {
  19.   c->x = a->x+b->x;  c->y = a->y+b->y;    c->z = a->z+b->z;
  20.   return(c);
  21.   }
  22.  
  23. /* return the dot product of vectors a and b */
  24. double V3Dot(Vector3 *a, Vector3 *b) {
  25.   return((a->x*b->x)+(a->y*b->y)+(a->z*b->z));
  26.   }
  27. #endif
  28.  
  29. /* make a square height field of quadrilaterals and triangles */
  30. main(int ac, char *av[]) {
  31. int xres, yres;
  32. Smooth smooth;
  33.   if (ac < 3) { printf("use: test x y\n"); exit(-1); };     /* abrupt, I know */
  34.   xres = atoi(*++av);
  35.   yres = atoi(*++av);
  36.   smooth = initAllTables();        /* initialize */
  37.   buildMesh(smooth, xres, yres);    /* build the mesh (calls includePolygon) */
  38.   enableEdgePreservation(smooth, 0.0);    /* 90 degree folds or more stay crisp */
  39.   makeVertexNormals(smooth);        /* build the normals */
  40.   savePolys(smooth);            /* save the result in a file */
  41.   freeSmooth(smooth);            /* take only normals, leave only footprints */
  42.   }
  43.  
  44. /* z=f(x,y) */
  45. double fofxy(double x, double y) {
  46.    double h;
  47.    h = 2.0 * (0.5 - x); if (h < 0) h = -h; h = h * y;
  48.    return(h);
  49.    }
  50.  
  51. buildMesh(Smooth smooth, int xres, int yres) {
  52. int x, y;
  53. Point3 *vlist;
  54. double dx, dy, lx, ly, hx, hy;
  55.   vlist = NEWA(struct Point3Struct, 4);
  56.   dx = 1.0/((double)(xres));
  57.   dy = 1.0/((double)(yres));
  58.   for (y=0; y<yres; y++) {
  59.     ly = y * dy;
  60.     hy = (y+1) * dy;
  61.     for (x=0; x<xres; x++) {
  62.        lx = x * dx;
  63.        hx = (x+1) * dx;
  64.        if ((x+y)%2 == 0) addTriangles(lx, ly, hx, hy, vlist, smooth);
  65.            else addQuadrilateral(lx, ly, hx, hy, vlist, smooth);
  66.       };
  67.     };
  68.   free(vlist);
  69.   }
  70.  
  71. addTriangles(double lx, double ly, double hx, double hy,
  72.          Point3 *vlist, Smooth smooth) {
  73. Point3 *p = vlist;
  74.    /* make the first triangle */
  75.    p->x = lx;  p->y = ly;  p->z = fofxy(p->x, p->y); p++;
  76.    p->x = hx;  p->y = ly;  p->z = fofxy(p->x, p->y); p++;
  77.    p->x = lx;  p->y = hy;  p->z = fofxy(p->x, p->y); p++;
  78.    includePolygon(3, vlist, smooth, NULL);  /* add the polygon */
  79.    /* make the other triangle */
  80.    p = vlist;
  81.    p->x = hx;  p->y = ly;  p->z = fofxy(p->x, p->y); p++;
  82.    p->x = hx;  p->y = hy;  p->z = fofxy(p->x, p->y); p++;
  83.    p->x = lx;  p->y = hy;  p->z = fofxy(p->x, p->y); p++;
  84.    includePolygon(3, vlist, smooth, NULL);  /* add the polygon */
  85.    }
  86.  
  87. addQuadrilateral(double lx, double ly, double hx, double hy,
  88.          Point3 *vlist, Smooth smooth) {
  89. Point3 *p = vlist;
  90.    p->x = lx;  p->y = ly;  p->z = fofxy(p->x, p->y); p++;
  91.    p->x = hx;  p->y = ly;  p->z = fofxy(p->x, p->y); p++;
  92.    p->x = hx;  p->y = hy;  p->z = fofxy(p->x, p->y); p++;
  93.    p->x = lx;  p->y = hy;  p->z = fofxy(p->x, p->y); p++;
  94.    includePolygon(4, vlist, smooth, NULL);  /* add the polygon */
  95.    }
  96.  
  97. savePolys(Smooth smooth) {
  98. Polygon poly = smooth->polygonTable;
  99. int i, k;
  100. Point3 *v, *n;
  101.    printf("NQUAD\n");    /* header for point/normal format */
  102.    while (poly != NULL) {
  103.       for (i=0; i<4; i++) {
  104.      k = i;      /* we always write 4 points so double 3rd triangle vertex */
  105.      if (i >= poly->numVerts) k = poly->numVerts-1;
  106.      v = &(poly->vertices[k]);
  107.      n = &(poly->normals[k]);
  108.      printf("%f %f %f %f %f %f\n", v->x, v->y, v->z, n->x, n->y, n->z);
  109.      };
  110.       printf("\n");
  111.       poly = poly->next;
  112.       };
  113.    }
  114.